package com.activemq.maintance;

import cn.hutool.core.date.DateUtil;
import com.activemq.MqHelloWorld;
import lombok.extern.slf4j.Slf4j;
import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;
import java.util.ArrayList;
import java.util.Collections;

public class JdbcQueSender {
    public static final String QUEUE = "JDBC_QUEUE01";

    public static void main(String[] args) throws Exception {
        ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(MqHelloWorld.URL);
        Connection connection = factory.createConnection();
        connection.start();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Queue queue = session.createQueue(QUEUE);
        MessageProducer producer = session.createProducer(queue);
        //设置非持久化
        producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
        for (int i = 0; i < 3; i++) {
            producer.send(session.createTextMessage("测试发送JDBC消息：" + DateUtil.now() + "_" + i));
        }
        producer.close();
        session.close();
        connection.close();
    }
}

@Slf4j
class Rev {
    public static void main(String[] args) throws Exception {
        ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(MqHelloWorld.URL);
        Connection connection = factory.createConnection();
        connection.start();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Queue queue = session.createQueue(JdbcQueSender.QUEUE);
        MessageConsumer consumer = session.createConsumer(queue);
        consumer.setMessageListener((msg) -> {
            try {
                log.error("收到了来自XXX的消息！！！" + ((TextMessage) msg).getText());
            } catch (JMSException e) {
                throw new RuntimeException(e);
            }
        });
        System.in.read();
        consumer.close();
        session.close();
        connection.close();
    }
}

class NSender {
    public static final String QUEUE = "JDBC_QUEUE02";

    public static void main(String[] args) throws Exception {
        ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(MqHelloWorld.URL);
        Connection connection = factory.createConnection();
        connection.start();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Queue queue = session.createQueue(QUEUE);
        MessageProducer producer = session.createProducer(queue);
        //设置非持久化
        producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
        for (int i = 0; i < 3; i++) {
            producer.send(session.createTextMessage("测试发送JDBC消息：" + DateUtil.now() + "_" + i));
        }
        producer.close();
        session.close();
        connection.close();
    }
}